#!/bin/bash

# WiFi 配置
TARGET_WIFI="SCC_TEST_5G"

# 获取 WiFi 接口名称
wifi_interface=$(networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/,/Ethernet Address/' | awk -F': ' '/Device/{print $2}')
if [ -z "$wifi_interface" ]; then
    echo "未找到 WiFi 接口"
    exit 1
fi
echo "WiFi 接口名称是: $wifi_interface"

# 获取当前连接的 WiFi 名称
current_wifi=$(networksetup -getairportnetwork "$wifi_interface" | awk -F': ' '{print $2}')
if [[ "$current_wifi" == "You are not associated with an AirPort network." ]]; then
    echo "当前未连接到任何 WiFi 网络"
    current_wifi=""
else
    echo "当前连接的 WiFi 是: $current_wifi"
fi

# 切换到指定的 WiFi
echo "正在切换到 WiFi 网络: $TARGET_WIFI"
networksetup -setairportnetwork en1 "SCC_TEST_5G"
sleep 5  # 等待网络切换完成

# 切换到目标目录
echo "切换到目标目录..."
cd /Users/qihao/Documents/workspace/scan_pay || {
    echo "切换目录失败"
    exit 1
}

# 执行 git pull 命令
echo "正在执行 git pull 命令（25秒超时）"
(git pull & pid=$!; (sleep 25; kill -TERM $pid 2>/dev/null) & wait $pid) || echo "git pull 超时或失败，继续执行后续操作"

# 切换回原来的 WiFi
    #  echo "正在切换回原来的 WiFi 网络: $current_wifi"
    # networksetup -setairportnetwork en1 "Zimi"
    # sleep 5  # 等待网络切换完成
    networksetup -setairportpower en1 off && sleep 2 && networksetup -setairportpower en1 on